package com.focus.xyz.other.cases;

import com.focus.xyz.core.common.webdriver.DriverFactory;
import com.focus.xyz.core.utils.Constants;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;

import java.util.concurrent.TimeUnit;

/**
 * Created by cimon on 2016/3/16.
 */
public class GridDriverTest {

    private String baseUrl;
    private WebDriver driver;

    @Parameters({"browser","remoteUrl"})
    @BeforeClass(alwaysRun = true)
    public void setUp(String browser,String remoteUrl) throws Exception {
        baseUrl = "https://www.baidu.com/";
        if("firefox".equalsIgnoreCase(browser)){
            driver = DriverFactory.getRemoteDriver(remoteUrl).getBrowser(Constants.FIREFOX);
            driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);
        }else if("chrome".equalsIgnoreCase(browser)){
            driver = DriverFactory.getRemoteDriver(remoteUrl).getBrowser(Constants.CHROME);
            driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);
        }else if("ie".equalsIgnoreCase(browser)){
            driver = DriverFactory.getRemoteDriver(remoteUrl).getBrowser(Constants.IE);
            driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);
        }else{
            throw new IllegalArgumentException("The Browser Type is Undefined");
        }
    }

    @AfterClass(alwaysRun = true)
    public void tearDown(){
        if(driver!=null){
            driver.quit();
        }
    }

    @Test
    public void testRemoteDriver(){
        driver.get(baseUrl);
        driver.findElement(By.id("kw")).sendKeys("selenium");
        driver.findElement(By.id("su")).click();
        /*WebDriverWait wait = new WebDriverWait(driver,10);
        wait.until(new ExpectedCondition<Boolean>() {
            @Override
            public Boolean apply(WebDriver d){
                return d.findElement(By.id("")).getText().contains("");
            }
        });*/
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        Assert.assertEquals("selenium_百度搜索",driver.getTitle());
    }
}
